package misc

import (
	"dhfshop/controllers/base"
	"dhfshop/models/misc"
	"github.com/astaxie/beego/validation"
	"time"
	//"github.com/astaxie/beego"
)


//用户识别模块（User Identifier Module）
type UimodsController struct {
	base.BaseController
}

func (this *UimodsController) List() {
	query, _, fields, order_by, offset, limit, err := this.GetQueryPara()
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	var allowStoreId []int64
	if this.CurrentUser != nil {
		allowStoreId = this.CurrentUser.AllowStoreId
	}
	//beego.Debug("allowStoreId:", allowStoreId)
	l, cnt, err := misc.GetAllUimodss(allowStoreId, query, fields, order_by, offset, limit)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		TotalCount int64          `json:"total_count"`
		Uimods     []*misc.Uimods `json:"uimods"`
	}{TotalCount: cnt, Uimods: l}
	this.Data["json"] = r
	this.ServeJSON()
}

func (this *UimodsController) Post() {
	var v misc.Uimods
	valid := validation.Validation{}
	this.ParseForm(&v)
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	//v.UserId = this.Ctx.Input.GetData("curUserId").(int)
	valid.Required(v.StoreId, "store_id")
	valid.Required(v.AppId, "app_id")
	valid.Required(v.Types, "types")
	valid.Required(v.Name, "name")
	valid.Required(v.SubType, "subtype")
	valid.Required(v.Data, "data")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	_, err := misc.CreateUimods(&v)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		AppId   string    `json:"app_id"`
		Created time.Time `json:"created"`
	}{AppId: v.AppId, Created: time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *UimodsController) Put() {
	v := misc.Uimods{}
	this.ParseForm(&v)
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	valid := validation.Validation{}
	valid.Required(v.UimodsId, "uimods_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	updateFields, err := this.GetUpdateFields(v, "", "form", []string{""})
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if len(updateFields) > 0 {
		if err := misc.UpdateUimodsById(&v, updateFields...); err != nil {
			this.ServeError(err.Error())
			return
		}
	}
	rsp := struct {
		UimodsId int64     `json:"uimods_id"`
		Modified time.Time `json:"modified"`
	}{UimodsId: v.UimodsId, Modified: time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *UimodsController) Delete() {
	uimodsId, err := this.GetInt("uimods_id")
	if err != nil {
		this.ServeError("uimods_id is invalid")
		return
	}
	if err := misc.DeleteUimodsById(int64(uimodsId)); err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		UimodsId int       `json:"uimods_id"`
		Modified time.Time `json:"modified"`
	}{UimodsId: uimodsId, Modified: time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}
